global FIRST_YEAR 1964
global FINAL_YEAR 2016

/*
* 1a. job separation probability
*/
freduse USREC, clear
gen datem = mofd(daten)
format datem %tm

merge 1:1 datem using "${dropbox_dir}combined_results_yu\data\unemployment flow rates\unemp_flow_rates.dta"
keep if _merge==3
drop _merge
sum s if datem>=ym(${FIRST_YEAR},1) & datem<=ym(${FINAL_YEAR},12)
sum s if datem>=ym(${FIRST_YEAR},1) & datem<=ym(${FINAL_YEAR},12) & USREC==0
sum s if datem>=ym(${FIRST_YEAR},1) & datem<=ym(${FINAL_YEAR},12) & USREC==1

tsset datem
tsfilter hp s_hp = s if datem>=ym(${FIRST_YEAR},1) & datem<=ym(${FINAL_YEAR},12), smooth(14400)

sum s_hp if datem>=ym(${FIRST_YEAR},1) & datem<=ym(${FINAL_YEAR},12)

/*
* 2a. curvature of matching function
*/
freduse UNRATE, clear
gen datem = ym(year(daten), month(daten))
format datem %tm
keep datem UNRATE

merge 1:1 datem using "${dropbox_dir}combined_results_yu\data\HWI_barnichon.dta"
keep if _merge==3
drop _merge

merge 1:1 datem using "${dropbox_dir}combined_results_yu\data\unemployment flow rates\unemp_flow_rates.dta"
keep if _merge==3
drop _merge

gen tightness = HWI/UNRATE

keep if datem>=ym(${FIRST_YEAR},1) & datem<=ym(${FINAL_YEAR},12)
nl (F = (1+tightness^(-{iota}))^(-1/{iota})), initial(iota 1)

/*
* 3. ARMA(1,1) inflation process
*/

freduse CPIAUCSL PCE PCEPI, clear
gen datem = ym(year(daten), month(daten))
format datem %tm
tsset datem 
keep datem CPIAUCSL PCE PCEPI

gen cons_real = PCE*100/PCEPI
gen cons_grow = log(cons_real) - log(L.cons_real)
gen INF = log(CPIAUCSL) - log(L.CPIAUCSL)

keep if datem>=ym(${FIRST_YEAR},1) & datem<=ym(${FINAL_YEAR},12)

// consumption innovations, ar(1)
arima cons_grow L.cons_grow
predict double cons_grow_innov, residuals

// arma(1,1) inflation process
arima INF L.INF cons_grow_innov, ma(1)

/*
* 4. unemployment moments (quarterly average of monthly)
*/
freduse UNRATE, clear
gen dateq = yq(year(daten), quarter(daten))
format dateq %tq
keep dateq UNRATE

collapse (mean) UNRATE, by(dateq)

keep if dateq>=yq(${FIRST_YEAR},1) & dateq<=yq(${FINAL_YEAR},4)

tsset dateq

tsfilter hp UNRATE_hp = UNRATE, smooth(1600)

sum UNRATE UNRATE_hp

/*
* 5a. labor market tightness
*/

freduse UNRATE, clear
gen datem = ym(year(daten), month(daten))
format datem %tm
gen dateq = yq(year(daten), quarter(daten))
keep datem dateq UNRATE

merge 1:1 datem using "${dropbox_dir}combined_results_yu\data\HWI_barnichon.dta"
keep if _merge==3
drop _merge

keep if datem>=ym(${FIRST_YEAR},1) & datem<=ym(${FINAL_YEAR},12)
gen tightness = HWI/UNRATE

// take quarterly average
collapse (mean) tightness UNRATE, by(dateq)

tsset dateq
tsfilter hp tightness_hp = tightnes, smooth(1600)
tsfilter hp UNRATE_hp = UNRATE, smooth(1600)

sum tightness tightness_hp
corr tightness_hp UNRATE_hp

/*
6. Aggregate real consumption growth
*/

freduse A794RX0Q048SBEA, clear
rename A794RX0Q048SBEA Cons
gen dateq = yq(year(daten), quarter(daten))
format dateq %tq

tsset dateq
gen ConsGrowth = log(Cons) - log(L.Cons)

keep if dateq>=yq(${FIRST_YEAR},1) & dateq<=yq(${FINAL_YEAR},4)

sum ConsGrowth
corr ConsGrowth L.ConsGrowth

/*
7. Wage volatility, vol(wage bill)/vol(output)
* See Favilukis and Lin
* We directly use their numbers
*/

/*
* 8. Output volatility]
* Real gross domestic product per capita (A939RX0Q048SBEA)
*/

freduse A939RX0Q048SBEA, clear
gen dateq = qofd(daten)
format dateq %tq
rename A939RX0Q048SBEA Y
keep dateq Y

keep if dateq>=yq(${FIRST_YEAR},1) & dateq<=yq(${FINAL_YEAR},4)

tsset dateq

gen y = log(Y)
gen dy = y - L.y

sum dy
corr dy L.dy

tsfilter hp y_hp = y, smooth(600)

reg y_hp L.y_hp

gen dy_hp = y_hp - L.y_hp

sum y_hp
corr y_hp L.y_hp
corr dy_hp L.dy_hp

/*
* Dividend growth volatility
* crsp_returns.dta is available at monthly frequency
* Consumer Price Index for All Urban Consumers: All Items in U.S. City Average (CPIAUCSL)
*/

freduse CPIAUCSL, clear
gen datem = mofd(daten)
format datem %tm
rename CPIAUCSL CPI
keep datem CPI

merge 1:1 datem using "${dropbox_dir}combined_results_yu\data\crsp_returns.dta"
keep if _merge==3
drop _merge

keep datem ret retx CPI

tsset datem 

gen Dt_Ptminus1 = ret - retx

gen P = .

sort datem

replace P = 1 if _n==1
replace P = L.P*(1 + retx) if _n>1

gen D_nominal = Dt_Ptminus1*L.P

// deflate dividends
gen D = D_nominal*100/CPI

keep if datem>=ym(${FIRST_YEAR},1) & datem<=ym(${FINAL_YEAR},12)

gen year = year(dofm(datem))

collapse (sum) D, by(year)

tsset year

gen dlogD = log(D) - log(L.D)

sum dlogD